<?php
	include "../public/commom/config.php";//引入配置连接数据库文件
	include "../public/commom/commom.php";//引入配置图片文件
	include "../public/commom/timename.php";//引入配置图片文件
	
	if(!isset($_SESSION["user_id"])) {
		header("Location: ../index.php");//跳转页面
		exit;
	}
	
	if($_GET) {
		$tuser_id = $_GET["tuser_id"];
		$tusername = $_GET["tusername"];
	}
	
	$user_id = isset($_SESSION["user_id"]) ? $_SESSION["user_id"] : "";//用户id
	$username = isset($_SESSION["username"]) ? $_SESSION["username"] : "";//昵称
	$headportrait = isset($_SESSION["headportrait"]) ? $_SESSION["headportrait"] : "";//头像
	$iframesrc = isset($tuser_id) ? "?tuser_id=" . $tuser_id . "&tusername=" . $tusername : "";
?>

<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
	<meta charset="UTF-8"/>
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
	<meta name="renderer" content="webkit">
	<title>简易微博 私信</title>
	<!--[if lt IE 9]>
        <script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
        <script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
	<script type="text/javascript" src="../js/jquery-1.11.3.min.js" ></script>
	<script type="text/javascript" src="../js/bootstrap.min.js"></script>>
	<script src="../js/index.js" type="text/javascript" charset="utf-8"></script>
	<link rel="stylesheet" href="../css/bootstrap.min.css" />
	<link rel="stylesheet" href="../css/set.css" />
	<script type="text/javascript">
		function header(uid) {
			$.post("header.php", {
				user_id: uid
			}, function(data, status) {
				if(data.poor) {
					for (var i = data.poor - 1; i >= 0; i--) {
						var txt = "<li><a href='right.php?tuser_id=" + data.headlist[i]["tuid"] + "&tusername=" + data.headlist[i]["username"] + "' target='right'><img src='<?php echo $imgPath; ?>" + data.headlist[i]["headportrait"] + "'/></a><span class='badge unreadnum'>" + data.headlist[i]["unread"] + "</span></li>";
						$(".c_user_hear ul").append(txt);
					}
				}
		    }, "json");
		}
		
		setInterval(function () {
			header(<?php echo $user_id; ?>);
			message(<?php echo $user_id; ?>);
		}, 2000);
	</script>
</head>
<body>

<nav class="navbar navbar-default navbar-fixed-top ks_nav" role="navigation">
	<div class="container">
		<div class="navbar-header">
			<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#ks_nav">
				<span class="icon-bar"></span>
	        	<span class="icon-bar"></span>
	        	<span class="icon-bar"></span>
      		</button>
			<a class="navbar-brand" href="../index.php">简易微博 - Beta v1.0.0</a>
		</div>

		<div class="collapse navbar-collapse" id="ks_nav">
			<form class="navbar-form navbar-left" role="search">
				<div class="form-group">
					<input type="text" class="form-control" placeholder="找人...">
				</div>
				<button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-zoom-in"></span></button>
			</form>
			<ul class="nav navbar-nav navbar-right">
				<li><a href="userset.html">注册</a></li>
				<?php
					if($user_id) {
						echo "<li class='nav_user'><a href='userall.php?user_id=" . $user_id . "'><span class='glyphicon glyphicon-user'></span>" . $username . "</a></li>
							<li><a href='../cancellation.php'>登出</a></li>";
					}else {
						echo "<li><a href='#' data-toggle='modal' data-target='#useron'>登陆</a></li>";
					}
				?>
				<li><a href="#">关于作者</a></li>
				<li><a href="#">返回顶部</a></li>
			</ul>
		</div>
	</div>
</nav>

<div class="container">
	<div class="row">
		
		<div class="col-md-3 col-md-push-9">
			<?php
				if($user_id) {
					?>
						
						<!-- 登陆后 -->
						<div class="cont top_right">
							<div class="row">
								<div class="col-md-6 col-md-offset-3">
									<a href="userall.php"><img src="<?php echo $imgPath . $headportrait; ?>" width="100%" class="img-circle hidden-xs"></a>
								</div>
								<div class="col-md-12">
									<h5 class="text-center"><a href="userall.php"><?php echo $username; ?></a></h5>
								</div>
							</div>
							<div class="row">
								<div class="col-md-12">
									<?php
										$sql = "SELECT privateletter.* FROM privateletter LEFT JOIN user ON (privateletter.user_id = user.id) WHERE privateletter.user_id = '$user_id' AND privateletter.status = '0'";//sql语句 - 查找该用户所有私信数量
										
										$result = mysqli_query($conn, $sql);//result - 执行sql语句
										
//										$prinum = mysqli_num_rows($result);//每个用户私信数量
									?>
<!--									<a class="btn btn-info btn-block" href="">私信 <span class="badge prinum">--><?php //if($prinum != 0) { echo $prinum; } ?><!--</span></a>-->
                                    <a class="btn btn-info btn-block" href="">私信 <span class="badge prinum"></span></a>
								</div>
								<div class="col-md-12">
									<?php
										$sql = "SELECT comment.* FROM comment LEFT JOIN user ON (comment.user_id = user.id) LEFT JOIN weibo ON (comment.weibo_id = weibo.id) WHERE weibo.user_id = '$user_id'";//sql语句 - 查找该用户所有评论数量
										
										$result = mysqli_query($conn, $sql);//result - 执行sql语句
										
										$comnum = mysqli_num_rows($result);//每个用户评论数量
										
										
										mysqli_free_result($result);//释放内存
									?>
									<a class="btn btn-info btn-block" href="">评论 <span class="badge comnum"><?php if($comnum != 0) { echo $comnum; } ?></span></a>
								</div>
							</div>
						</div>
						
					<?php
				}else {
					?>
						
						<!-- 登陆前 -->
						<div class="cont top_right user_on">
							<h4 class="text-center">登陆 / 注册</h4>
							<div class="row">
								<div class="col-md-12">
									<a class="btn btn-info btn-block" href="#" data-toggle="modal" data-target="#useron">登&nbsp;&nbsp;陆</a>
								</div>
								<div class="col-md-12">
									<a class="btn btn-info btn-block" href="userset.html">注&nbsp;&nbsp;册</a>
								</div>
							</div>
						</div>
						
					<?php
				}
			?>
			<!-- /.cont_right -->
		</div>
		
		<div class="col-md-1 col-md-pull-2">
			<div class="c_user_hear">
				<ul>
					<?php
						$sql = "SELECT * FROM (
(SELECT privateletter.*, privateletter.tuser_id tuid, user.headportrait, user.username FROM privateletter LEFT JOIN user ON (privateletter.tuser_id = user.id) WHERE privateletter.user_id = '$user_id')
UNION 
(SELECT privateletter.*, privateletter.user_id tuid, user.headportrait, user.username FROM privateletter LEFT JOIN user ON (privateletter.user_id = user.id) WHERE privateletter.tuser_id = '$user_id')
) AS c
GROUP BY c.tuid";//sql语句 - 查找自己所有私信的用户
						
						$result = mysqli_query($conn, $sql);//result - 执行sql语句
						
						$headnum = mysqli_num_rows($result);//私信用户数量
						
						$_SESSION["cheadnum"] = $headnum;//私信用户数量
						
						while($row = mysqli_fetch_array($result)) {
							$sql1 = "SELECT privateletter.* FROM privateletter LEFT JOIN user ON (privateletter.user_id = user.id) WHERE privateletter.user_id = '$user_id' AND privateletter.tuser_id = " . $row["tuid"] . " AND privateletter.status = '0'";//sql语句 - 查找该用户未读私信数量
							
							$result1 = mysqli_query($conn, $sql1);//result - 执行sql语句
							
							if(mysqli_num_rows($result1)) {
								$_SESSION["unread"][$row["tuid"]]["message"] = mysqli_num_rows($result1);//该用户未读私信数量
								//$unreadnum = mysqli_num_rows($result1);//未读私信数量
							}else {
								$_SESSION["unread"][$row["tuid"]]["message"] = "";//该用户未读私信数量
							}
							
							echo "<li><a href='right.php?tuser_id=" . $row["tuid"] . "&tusername=" . $row["username"] . "' target='right'><img src='" . $imgPath . $row["headportrait"] . "'/></a><span class='badge unreadnum'>" . $_SESSION["unread"][$row["tuid"]]["message"] ."</span></li>";
						}
						
						mysqli_free_result($result);//释放内存
						
						mysqli_close($conn);//关闭数据库连接
					?>
				</ul>
			</div>
		</div>
		
		<div class="col-md-7 col-md-pull-2">
			<!-- /.cont_left -->
			
			<div class="pri_right">
				<iframe src="right.php<?php echo $iframesrc; ?>" name="right" width="100%" onload="this.height=this.contentWindow.document.body.scrollHeight"></iframe>
			</div>
			<!-- /.top_left -->
		</div>

	</div>
</div>

<!-- 用户登陆弹框 -->
<div class="modal fade" id="useron" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
				<h4 class="modal-title" id="myModalLabel">用户登陆</h4>
			</div>
			<form action="login.php" method="post" role="form">
				<div class="modal-body">
					<div class="form-group">
						<label>账号：</label>
						<input type="text" name="account" class="form-control" placeholder="请输入您的账号..." autocomplete="off">
					</div>
					<div class="form-group">
						<label>密码：</label>
						<input type="password" name="password" class="form-control" placeholder="请输入您的密码..." autocomplete="off">
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<input type="submit" class="btn btn-primary" value="登陆">
				</div>
			</form>
		</div>
	</div>
</div>

</body>
</html>
